/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <QCoreApplication>
#include <QDate>
#include <QFile>
#include <QMessageBox>
#include <QSet>

#include "purgeold.h"

#include <billitem.h>

Q_EXPORT_PLUGIN2(plugin_purgeold, PurgeOld)

PurgeOld::PurgeOld()
{
    m_lib = 0;

    m_actionRun = new QAction(tr("Purge Old"), 0);
}

PurgeOld::~PurgeOld()
{
    delete m_actionRun;
}

void PurgeOld::setLib(NiwBillManager *lib)
{
    m_lib = lib;
}

QAction *PurgeOld::getAction()
{
    return m_actionRun;
}

QString PurgeOld::getName()
{
    return tr("Purge Old");
}

QString PurgeOld::getCredits()
{
    // Default credits for the application.
    // Included in niw-bill-manager app as a resource.
    QFile creditsFile(":/plugin-purgeold/about/credits.html");
    creditsFile.open(QIODevice::ReadOnly);
    QString credits = creditsFile.readAll();
    creditsFile.close();

    return credits;
}

QString PurgeOld::getDescription()
{
    return tr("Delete all old bills that are paid.");
}

QString PurgeOld::getVersion()
{
    return "1.0.1";
}

QString PurgeOld::getLicense()
{
    // Default license of the application.
    // Included in niw-bill-manager app as a resource.
    QFile licenseFile(":/plugin-purgeold/about/gpl-3.0-standalone.html");
    licenseFile.open(QIODevice::ReadOnly);
    QString license = licenseFile.readAll();
    licenseFile.close();

    return license;
}

bool PurgeOld::canConfig()
{
    return false;
}

void PurgeOld::execConfigDialog()
{
}

void PurgeOld::on_actionRun_triggered()
{
    if (!m_lib) {
        QMessageBox::critical(0, tr("Error"), tr("Lib not loaded.\n"
            "Cannot query bills."));
        return;
    }

    // A list of old / paid bills to delete.
    QSet<QString> billIds;

    // Get all the bills to find the old ones.
    m_lib->startGetAllBillItems();
    while (m_lib->hasMoreBillItems()) {
        BillItem billItem;

        if (m_lib->getNextBillItem(billItem)) {
            if (billItem.getDateDue() < QDate::currentDate()
                && billItem.getPaid() == true
                && billItem.getPaymentRecurringPeriod()
                == RecurringPeriodType::NONE)
            {
                billIds.insert(billItem.getId());
            }
        }
    }

    if (!billIds.isEmpty()) {
        Q_FOREACH (QString id, billIds) {
            m_lib->deleteBillItem(id);
        }
    }

    emit uiUpdate();
}
